package com.example.gifttalk0.rank.item;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.gifttalk0.R;
import com.example.gifttalk0.entity.RankItemEntity;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by lenovo on 2017/6/29.
 */

public class RankGridAdapter extends BaseAdapter {

    private List<RankItemEntity> items;
    private Context mContext;
    private LayoutInflater mInflater;

    public RankGridAdapter (Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        items = new ArrayList<>();
    }

    public void addAll(List<RankItemEntity> items){
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    @Override
    public int getCount () {
        return items.size();
    }

    @Override
    public RankItemEntity getItem (int position) {
        return items.get(position);
    }

    @Override
    public long getItemId (int position) {
        return 0;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_grid_rank, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        RankItemEntity item = getItem(position);
        Glide.with(mContext).load(item.getCover_image_url()).into(holder.imgCover);
        holder.textShortDescription.setText(item.getShort_description());
        holder.textName.setText(item.getName());
        holder.textPrice.setText("¥ " + item.getPrice());
        return convertView;
    }

    public static class ViewHolder{
        private ImageView imgCover;
        private TextView textShortDescription;
        private TextView textName;
        private TextView textPrice;

        public ViewHolder (View itemView) {
            imgCover = (ImageView) itemView.findViewById(R.id.img_cover);
            textShortDescription = (TextView) itemView.findViewById(R.id.text_short_description);
            textName = (TextView) itemView.findViewById(R.id.text_name);
            textPrice = (TextView) itemView.findViewById(R.id.text_price);

        }
    }
}
